orange.datadoctor = {
	// 添加医生信息页面
	add_dataDoctor:function(){
		$('#addDataDoctorWin').window('open');
		$('#addDataDoctorForm').form('clear');
		$('#addDoc_loading').attr('src','resources/images/default.jpg');
	},
	//添加医生信息页面保存按钮
	addSaveOK: function() {
		var jsonData = orange.systemPage.getFormData('addDataDoctorForm');
		jsonData['orgGuid'] = orange.getOrganizationData().id;
		jsonData['workTime'] = $('#add_workTime').combobox('getValues').toString();
		jsonData['licenseTypeName'] = $('#update_doctorLicenseType').combobox('getText');
		jsonData['doctorTitleName'] = $('#update_doctorTitle').combobox('getText');
		jsonData['eduBkName'] = $('#update_doctorEduBk').combobox('getText');
		jsonData['educationName'] = $('#update_doctorEducation').combobox('getText');
	    var submitFlag = $('#addDataDoctorWin').form('validate'); 
		if(submitFlag==true){
			var add_inDate = $('#add_inDate').datebox('getValue');
			add_inDate = add_inDate.replace(/\-/gi,"/");
			var inDatemill = Date.parse(add_inDate);
			var add_birthday = $('#add_birthday').datebox('getValue');
			add_birthday = add_birthday.replace(/\-/gi,"/");
			var birthdaymill = Date.parse(add_birthday);
            if(birthdaymill > inDatemill){
            	$.messager.alert('提示','入馆时间不能早于出生日期！');
            	return ;
            }
            var now = new Date();
            if(now < inDatemill){
            	$.messager.alert('提示','入馆时间不能迟于当前时间！');
            	return ;
            }
            if(now < birthdaymill){
            	$.messager.alert('提示','出生日期不能迟于当前日期！');
            	return ;
            }
 			$.ajax({
		        url:'doctor/add',
		        type: 'post',
		        data: jsonData,
		        success: function(data){
		        	if(data.success == true){
		        		$("#doctor_guid").val(data.data.guid);
		        		$.messager.show({
		        			title:'提示',
		        			msg:'保存成功！'
		        		});
			            $('#org_dataDoctorGrid').datagrid('reload');    
		        	}else{
		        		$.messager.alert('提示',data.errorMsg);
		        	}
		        }
		    });
		}else{
			$.messager.alert('我的消息','不能保存，还有必填项未填写！');
			return;
		}
	},
	
	//添加医生信息页面取消按钮
	addCancelOK: function () {
		$('#addDataDoctorWin').window('close');
	},
	nofind:function(){
		$('#updateDoc_loading').attr('alt','图片不存在');
	},
	//修改医生信息页面
	update_dataDoctor: function () {
		$('#updateDataDoctorForm').form('clear');
		var rowData= $("#org_dataDoctorGrid").datagrid("getSelected");
		if(rowData==null || rowData==""){
			$.messager.alert('提示','请选择需要修改的记录！'); 
		}else{
			var docId = rowData.docId;
			$.ajax({
				url:'doctor/getDataDoctorDataById',
				data:{doctorId:docId},
				aysnc:false,
				success:function(data){
					$('#updateDataDoctorForm').form('load',data);
					if(data.photoPath=="" || data.photoPath==null){
						$('#updateDoc_loading').attr('src','resources/images/default.jpg');
					}else{
						$('#updateDoc_loading').attr('src',data.photoPath);
					}
					$('#update_dataDocRightGrid').datagrid({
						url:'diseaseType/getDisNamesByDisIds',
						queryParams:{disIds:data.doctorSpeciality},
						onLoadSuccess:function(){
							orange.datadoctor.loadUpdateDoctorSpecial();
						}
					});
				}
			});
			$('#updateDataDoctorWin').window('open');
		}
	},
	//修改医生信息页面保存按钮
	updateSaveOK: function() {
		var jsonData = orange.systemPage.getFormData('updateDataDoctorForm');
		jsonData['guid'] = $('#doctor_docId').val();
		jsonData['workTime'] = $('#update_workTime').combobox('getValues').toString();
		jsonData['org_guid'] = orange.getOrganizationData().id;
		jsonData['licenseTypeName'] = $('#update_doctorLicenseType').combobox('getText');
		jsonData['doctorTitleName'] = $('#update_doctorTitle').combobox('getText');
		jsonData['eduBkName'] = $('#update_doctorEduBk').combobox('getText');
		jsonData['educationName'] = $('#update_doctorEducation').combobox('getText');
		var update_inDate = $('#update_inDate').datebox('getValue');
		update_inDate = update_inDate.replace(/\-/gi,"/");
		var inDatemill = Date.parse(update_inDate);
		var update_birthday = $('#update_birthday').datebox('getValue');
		update_birthday = update_birthday.replace(/\-/gi,"/");
		var birthdaymill = Date.parse(update_birthday);
        if(birthdaymill > inDatemill){
        	$.messager.alert('提示','入馆时间不能早于出生日期！');
        	return ;
        }
		var update_leaveDate = $('#update_leaveDate').datebox('getValue');
		update_leaveDate = update_leaveDate.replace(/\-/gi,"/");
		var update_leaveDatemill = Date.parse(update_leaveDate);
		var update_birthday = $('#update_birthday').datebox('getValue');
		update_birthday = update_birthday.replace(/\-/gi,"/");
		var birthdaymill = Date.parse(update_birthday);
        if(birthdaymill > update_leaveDatemill){
        	$.messager.alert('提示','离馆时间不能早于出生日期！');
        	return ;
        }
		$.ajax({
			url:'doctor/edit',
			type:'post',
			data:jsonData,
			success:function(data){
				if(data.success == true){
					$.messager.show({
						title:'提示',
						msg:'修改成功',
					});
					$('#org_dataDoctorGrid').datagrid('reload');
					$('#updateDataDoctorWin').window('close');
				}else{
					$.messager.alert('提示',data.errorMsg);
				}
			}
		});
	},
	//修改医生信息页面取消按钮
	updateCancelOK: function() {
		$('#updateDataDoctorWin').window('close');
	},
	//添加图片上传的选择操作
	addImageFileUpload: function() {
		if($("#doctor_guid").val() == null || $("#doctor_guid").val() == ""){
			$('#doctorImageFile').val('');
			$.messager.alert("提示","保存成功后才能上传照片");
		}else{
			$.ajaxFileUpload({  
		        url:'image/imageUpload',  
		        secureuri:false,  
		        fileElementId:'doctorImageFile',  
		        dataType: 'json',  
		        data:{
		        	orgId:orange.getOrganizationData().id,
		        	guid:$("#doctor_guid").val(),
		        	staffType:'1'
		        },  
		        success: function (data, status)  
		        {  
	        		$('#addDoc_loading').attr('src',data.data + "?" + new Date());
		        },  
		        error: function (data, status, e)  
		        {  
		            //这里处理的是网络异常，返回参数解析异常，DOM操作异常  
		            alert("上传发生异常");  
		        }  
		    });
		}
	},
	//修改医生图片上传
	updateImageFileUpload: function() {
		$.ajaxFileUpload({  
	        url:'image/imageUpload',  
	        secureuri:false,  
	        fileElementId:'updatedoctorImageFile',  
	        dataType: 'json',  
	        data:{
	        	orgId:orange.getOrganizationData().id,
	        	guid:$("#doctor_docId").val(),
	        	staffType:'1'
	        },  
	        success: function (data, status)  
	        {  
	        	$('#updateDoc_loading').attr('src',data.data);
	            
	        },  
	        error: function (data, status, e)  
	        {  
	            //这里处理的是网络异常，返回参数解析异常，DOM操作异常  
	            alert("上传发生异常");  
	        }  
	    });
	},
	loadUpdateDoctorSpecial: function() {
		 var data = $('#update_dataDocRightGrid').datagrid('getData').rows;
         var value = [];
         var text = '';
         if(data.length!==0) {
             for (var i = 0; i < data.length; i++) {
                 value.push(data[i].guid);
                 text += data[i].typeName;
                 if (i!==data.length-1) {
                     text += ',';
                 }
             };
         }
         $("#update_doctorSpeciality").combo('setValue',value.join(","));
         $("#update_doctorSpeciality").textbox('setText',text);
	}
};

/**
 * 医生表格的属性配置
 */
$('#org_dataDoctorGrid').datagrid({ //医生个人信息datagrid
	url:'doctor/getListBySearchCriteria2Page',
    singleSelect:true,
    rownumbers:true,
    queryParams:{doctorStatus:'0',orgId:orange.getOrganizationData().id},
    columns:[[
            {field:'docId',title:'主键',hidden:true}, 
			{field:'doctorName',title:'姓名',width:'8%',halign:'center'}, 
			{field:'gender',title:'性别',width:'8%',halign:'center',formatter:function(value){
	          	var value_other = value == null ? "" : value;
	          	value_other = value_other==1?"男":"女";
	        	return '<span title="' + value_other + '">' + value_other + '</span>';
	        }},  
			{field:'age',title:'年龄',width:'10%',halign:'center'},  
			{field:'licenseTypeName',title:'执照类型',width:'12%',halign:'center'},
			{field:'doctorTitleName',title:'职称',width:'10%',halign:'center'},
			{field:'eduBkName',title:'教育背景',width:'10%',halign:'center'},
			{field:'educationName',title:'学历',width:'10%',halign:'center'},
			{field:'indate',title:'入馆时间',width:'12%',halign:'center'},
			{field:'specialityNames',title:'医疗专长',width:'10%',halign:'center',formatter:function(value){
	          	var value_other = value == null ? "" : value;
	        	return '<span title="' + value_other + '">' + value_other + '</span>';
	        }},
			{field:'doctorStatus',title:'当前状态',width:'8%',halign:'center',formatter:function(value){
	          	var value_other = value == null ? "" : value;
	          	value_other = value_other==0?"在馆":"离馆";
	        	return '<span title="' + value_other + '">' + value_other + '</span>';
	        }},
			]],
			pagination:true,
			enableHeaderClickMenu: false
});
var arr1 = ["#add_eduBK", "#update_doctorEduBk"]; 
for(var i = 0;i < arr1.length; i++) {
	//教育背景 下拉框
	$(arr1[i]).combobox({    
		//url:'',    
		data:$.appDictCache.get('dict_doctoreducationbackg'),
		valueField:'code',    
	    textField:'value',
		panelHeight:'auto',
		width:'33%'
	});
};
var arr2 = ["#add_education", "#update_doctorEducation"]; 
for(var i = 0;i < arr2.length; i++) {
	//学历下拉框
	$(arr2[i]).combobox({    
		//url:'',    
		data:$.appDictCache.get('dict_education'),
		valueField:'code',    
	    textField:'value',
		panelHeight:'auto',
		width:'33%'
	});
};
var arr3 = ["#add_doctorTitle", "#update_doctorTitle"]; 
for(var i = 0;i < arr3.length; i++) {
	//职称下拉框
	$(arr3[i]).combobox({    
		//url:'',    
		data:$.appDictCache.get('dict_doctortechnicaltitle'),
		valueField:'code',    
	    textField:'value',
		panelHeight:'auto',
		width:'33%'
	});
};
var arr4 = ["#add_doctorType", "#update_doctorType"]; 
for(var i = 0;i < arr4.length; i++) {
	//医生类型下拉框
	$(arr4[i]).combobox({    
		data:$.appDictCache.get('dict_doctortype'),
		width:'33%',
		valueField:'code',    
		textField:'value', 
		panelHeight:'auto',
		pagination:true,
	});
};
var arr5 = ["#add_licenseType", "#update_doctorLicenseType"]; 
for(var i = 0;i < arr5.length; i++) {
	//医生执照类型下拉框
	$(arr5[i]).combobox({    
		//url:'',    
		data:$.appDictCache.get('dict_doctorlicensestype'),
		valueField:'code',    
	    textField:'value',
		panelHeight:'auto',
		width:'33%'
	});
};
var arr6 = ["#add_workTime", "#update_workTime"]; 
for(var i = 0;i < arr6.length; i++) {
	//医生出诊时间下拉框
	$(arr6[i]).combobox({   
		data:[{'code':'1','name':'周一下午'},{'code':'2','name':'周二下午'},{'code':'3','name':'周三下午'}],
		width:'81%',
		valueField:'code',    
		textField:'name', 
		panelHeight:'auto',
		multiple:true,
		required:true
		//required:true
	});
};
/**
 * 添加医生--医疗专长的下拉框
 */
$('#add_doctorSpeciality').combobox({
	width:'81%',
	editable: false,
	panelWidth: 485,
	panelHeight:285,
	onHidePanel: function() {
		 var data = $('#add_dataDocRightGrid').datagrid('getData').rows;
         var value = [];
         var text = '';
         if(data.length!==0) {
             for (var i = 0; i < data.length; i++) {
                 value.push(data[i].guid);
                 text += data[i].typeName;
                 if (i!==data.length-1) {
                     text += ',';
                 }
             };
         }
         $("#add_doctorSpeciality").combo('setValue',value.join(","));
         $("#add_doctorSpeciality").textbox('setText',text);
	}
});
$('#add_doctorSP').appendTo($('#add_doctorSpeciality').combo('panel'));
/**
 * 医疗专长搜索框定义
 */
$('#add_dataDocSearch').searchbox({ 
	prompt:'描述搜索',
	width:172,
	searcher:function(value,name){ 
		$('#add_dataDocLeftGrid').datagrid('load',{
			searchstr : value,
		});
	}
}); 
/*
 * 医疗专长左边的数据表格配置
 */
$('#add_dataDocLeftGrid').datagrid({
	url:'diseaseType/findDiseaseTypebyPage',
	singleSelect: true,
	width:275,
	height:250,
	pagination: true, //是否分页
	pageSize: 6, //每页显示的记录条数，默认为10 
	pageList:[6],
	columns: [[
        {field: 'guid',title: '编码',hidden:true},
		{field: 'typeParent',title: '标准',width: 60},
		{field: 'typeName',title: '名称',width: 100},
		{field: 'typeDescribe',title: '描述',width: 100}
	]],
	onSelect: function(rowIndex, rowData) {
		var data = $('#add_dataDocRightGrid').datagrid('getData').rows;
		var flag = true;
		if(data.length!==0) {
			for (var i = 0; i < data.length; i++) {
				if(data[i].guid==rowData.guid) {
					flag = false;
				}
			};
		}
		if (flag && data.length<5) {
			$('#add_dataDocRightGrid').datagrid('insertRow',{
				index:0,
				row:rowData
			});
		}
	}
});
/**
 * 医疗专长已选择的数据表格配置
 */
$('#add_dataDocRightGrid').datagrid({
	singleSelect: true,
	width:185,
	height:250,
    columns: [[
        {field: 'guid',title: '编码',hidden:true},
		{field: 'typeParent',title: '标准',width: 60},
		{field: 'typeName',title: '名称',width: 120},
	]],
	onSelect: function(rowIndex, rowData) {
		$('#add_dataDocRightGrid').datagrid('deleteRow',rowIndex);
	}
});

/**
 * 修改医生--医疗专长的下拉框
 */
$('#update_doctorSpeciality').combobox({
	width:'81%',
	editable: false,
	panelWidth: 485,
	panelHeight:285,
	onHidePanel: function() {
		orange.datadoctor.loadUpdateDoctorSpecial();
	}
});
$('#update_doctorSP').appendTo($('#update_doctorSpeciality').combo('panel'));
/**
 * 医疗专长搜索框定义
 */
$('#update_dataDocSearch').searchbox({ 
	prompt:'描述搜索',
	width:172,
	searcher:function(value,name){ 
		$('#update_dataDocLeftGrid').datagrid('load',{
			searchstr : value,
		});
	}
}); 
/*
 * 医疗专长左边的数据表格配置
 */
$('#update_dataDocLeftGrid').datagrid({
	url:'diseaseType/findDiseaseTypebyPage',
	singleSelect: true,
	width:275,
	height:250,
	pagination: true, //是否分页
	pageSize: 6, //每页显示的记录条数，默认为10 
	pageList:[6],
	columns: [[
        {field: 'guid',title: '编码',hidden:true},
		{field: 'typeParent',title: '标准',width: 60},
		{field: 'typeName',title: '名称',width: 100},
		{field: 'typeDescribe',title: '描述',width: 100}
	]],
	onSelect: function(rowIndex, rowData) {
		var data = $('#update_dataDocRightGrid').datagrid('getData').rows;
		var flag = true;
		if(data.length!==0) {
			for (var i = 0; i < data.length; i++) {
				if(data[i].guid==rowData.guid) {
					flag = false;
				}
			};
		}
		if (flag && data.length<5) {
			$('#update_dataDocRightGrid').datagrid('insertRow',{
				index:0,
				row:rowData
			});
		}
	}
});
/**
 * 医疗专长已选择的数据表格配置
 */
$('#update_dataDocRightGrid').datagrid({
	singleSelect: true,
	width:185,
	height:250,
    columns: [[
        {field: 'guid',title: '编码',hidden:true},
		{field: 'typeParent',title: '标准',width: 60},
		{field: 'typeName',title: '名称',width: 120},
	]],
	onSelect: function(rowIndex, rowData) {
		$('#update_dataDocRightGrid').datagrid('deleteRow',rowIndex);
	}
});
